//
//  XLScheduleStatusView.m
//  industry
//
//  Created by xl on 2020/12/21.
//  Copyright © 2020 Haier. All rights reserved.
//

#import "XLScheduleStatusView.h"
#import "NSString+XL.h"
#import "XLScheduleStatusItemView.h"

@interface XLScheduleStatusView ()

@property (nonatomic, strong) XLImageView *iconIG;
@property (nonatomic, strong) XLScrollView *scrollView;

@property (nonatomic, assign) NSInteger currentIndex;


@end
@implementation XLScheduleStatusView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.iconIG];
        [self addSubview:self.scrollView];
        
        [_iconIG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(self).offset(25);
            make.size.mas_equalTo(CGSizeMake(19, 18));
        }];
        
        [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.iconIG.mas_right).offset(10);
            make.centerY.equalTo(self);
            make.size.mas_offset(CGSizeMake(self.width - 25 - 10 - 19, 40));
        }];
    }
    return self;
}

#pragma mark - Public
- (void)loadItemsDataArr:(NSArray *)dataArr {
    self.currentIndex = 0;
    [_scrollView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    CGFloat offset_X = 0;
    for (int i = 0; i < dataArr.count; i ++) {
        NSDictionary *temDic = dataArr[i];
        NSString *title = [temDic objectForKey:@"title"];
        NSNumber *num = [temDic objectForKey:@"num"];
        CGFloat width = ((num.intValue > 0) ? 38:20) + [title calculateRowWidthWithFont:[UIFont fontOfPingFangSCRegularWithSize:13] height:20];
        XLScheduleStatusItemView *itemView = [[XLScheduleStatusItemView alloc] initWithFrame:CGRectMake(offset_X, 4, width, 32)];
        itemView.tag = 1000 + i;
        if (i == 0) {
            [itemView loadItemDataStatus:YES title:title num:num.integerValue];
        } else {
            [itemView loadItemDataStatus:NO title:title num:num.integerValue];
        }
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickItemView:)];
        [itemView addGestureRecognizer:tap];
        [_scrollView addSubview:itemView];
        offset_X += (width + 10);
    }
    _scrollView.contentSize = CGSizeMake(offset_X, 40);
}

- (void)clickItemView:(UITapGestureRecognizer *)sender {
    NSInteger tag = sender.view.tag - 1000;
    if (self.currentIndex == tag) {
        return;
    }
    XLScheduleStatusItemView *lastItem = [_scrollView viewWithTag:(1000 + self.currentIndex)];
    [lastItem changeViewStatus:NO];
    
    XLScheduleStatusItemView *currentItem = (XLScheduleStatusItemView *)sender.view;
    [currentItem changeViewStatus:YES];
    _currentIndex = tag;
    if (self.delegate && [self.delegate respondsToSelector:@selector(scheduleStatusSelectIndex:)]) {
        [self.delegate scheduleStatusSelectIndex:tag];
    }
}

#pragma mark - Setter/getter
- (XLImageView *)iconIG {
    if (!_iconIG) {
        _iconIG = [XLImageView new];
        _iconIG.image = XLIMAGE(@"sxzt");
    }
    return _iconIG;
}

- (XLScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [XLScrollView new];
    }
    return _scrollView;
}
@end
